package com.aysafety.iotdataplatformweb.services.winds.impl;

import com.aysafety.core.common.DateUtils;
import com.aysafety.iotdataplatformweb.dao.WindsDao;
import com.aysafety.iotdataplatformweb.services.winds.WindsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WindsServiceImpl implements WindsService {

    @Autowired
    WindsDao windsDao;

    @Override public long countByWinds(String mineId, String deviceId, String startTime, String endTime, int timeSpan,
        String timeUnit) throws Throwable {

        long startTimeTimestamp = DateUtils.fomatDateTime(startTime).getTime();
        long endTimeTimestamp = DateUtils.fomatDateTime(endTime).getTime();
        Map<String, Object> parram = new HashMap<String, Object>();
        parram.put("tableId", mineId + "." + "WINDSREALTIME");
        parram.put("entId", "\'" +mineId+ "\'");
        parram.put("deviceId", "\'" +deviceId+ "\'");
        parram.put("startTime", startTimeTimestamp);
        parram.put("endTime", endTimeTimestamp);
        return windsDao.countByWinds(parram);
    }

    @Override public List<Map<String, Object>> queryRealTimeByWinds(String mineId, String deviceId, String startTime,
        String endTime, int timeSpan, String timeUnit, int page, int size, String startRow) throws Throwable {
        long startTimeTimestamp = DateUtils.fomatDateTime(startTime).getTime();
        long endTimeTimestamp = DateUtils.fomatDateTime(endTime).getTime();
        Map<String, Object> parram = new HashMap<String, Object>();
        parram.put("tableId", mineId + "." + "WINDSREALTIME");
        parram.put("entId", "\'" +mineId+ "\'");
        parram.put("deviceId", "\'" +deviceId+ "\'");
        parram.put("startTime", startTimeTimestamp);
        parram.put("endTime", endTimeTimestamp);
        //parram.put("size", size);

        List<Map<String,Object>> result = windsDao.queryRealTimeByWinds(parram);
        for (int i = 0; i < result.size(); i++) {
            long timestamp = (long) result.get(i).get("datetime");
            Date dateTime = DateUtils.fomatDateTime(timestamp);
            result.get(i).put("datetime",DateUtils.getTime(dateTime));
            result.get(i).put("rowkey",result.get(i).get("PK"));
            result.get(i).remove("PK");
        }
        return result;
    }
}
